package com.cjlgb.design.upms.receiver;

import com.cjlgb.design.upms.api.channel.AccountOutputChannel;
import com.cjlgb.design.upms.api.entity.Oauth2Account;
import com.cjlgb.design.upms.api.entity.SysRole;
import com.cjlgb.design.upms.api.entity.SysUserInfo;
import com.cjlgb.design.upms.api.entity.SysUserRole;
import com.cjlgb.design.upms.channel.SysUserChannel;
import com.cjlgb.design.upms.service.SysRoleService;
import com.cjlgb.design.upms.service.SysUserRoleService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.stream.Collectors;

/**
 * @author WFT
 * @date 2019/9/1
 * description: 系统用户异步接口消费逻辑
 */
@Slf4j
@Component
@RequiredArgsConstructor
@EnableBinding(value = SysUserChannel.class)
public class SysUserReceiver {

    private final AccountOutputChannel accountOutputChannel;
    private final SysRoleService roleService;
    private final SysUserRoleService userRoleService;

    /**
     * 监听后台用户添加
     * @param userInfo 用户信息
     */
    @StreamListener(value = SysUserChannel.INPUT, condition = "headers['method']=='add'")
    public void listenerAdd(SysUserInfo userInfo, @Header(value = "method") String method){
        //  添加用户与角色间的关联
        this.buildRole(userInfo.getAccountId(),userInfo.getRoleIds());
        //  异步添加oauth2账号
        this.accountOutputChannel.output().send(
                //  默认设置默认密码
                MessageBuilder.withPayload(new Oauth2Account(userInfo,"123123"))
                        .setHeader("method", method).build()
        );
    }

    /**
     * 删除后台用户
     * @param accountIds 账号Id列表
     */
    @StreamListener(value = SysUserChannel.INPUT, condition = "headers['method']=='delete'")
    public void listenerDelete(Collection<Long> accountIds){
        //  删除用户与角色的关联
        this.userRoleService.deleteByAccountIds(accountIds);
        //  通知队列删除oauth2账号
        this.accountOutputChannel.output().send(
                MessageBuilder.withPayload(accountIds)
                        .setHeader("method","delete").build()
        );
    }

    /**
     * 监听后台用户绑定角色
     * @param userInfo 用户信息
     */
    @StreamListener(value = SysUserChannel.INPUT, condition = "headers['method']=='buildRole'")
    public void listenerBuildRole(SysUserInfo userInfo){
        //  删除用户与角色的关联
        this.userRoleService.deleteByAccountId(userInfo.getAccountId());
        //  添加用户与角色间的关联
        this.buildRole(userInfo.getAccountId(),userInfo.getRoleIds());
        //  更新账号缓存
        this.accountOutputChannel.output().send(
                MessageBuilder.withPayload(Collections.singletonList(userInfo.getAccountName()))
                        .setHeader("method", "deleteCache").build()
        );
    }

    private void buildRole(long accountId,Collection<Long> roleIds){
        //  获取角色Id列表
        roleIds = this.roleService.listByIds(roleIds)
                .stream().map(SysRole::getId).collect(Collectors.toList());
        if (!roleIds.isEmpty()) {
            //  添加用户与角色间的关联
            Collection<SysUserRole> userRoles = new ArrayList<>(roleIds.size());
            roleIds.forEach(roleId -> userRoles.add(new SysUserRole(accountId,roleId)));
            this.userRoleService.saveBatch(userRoles);
        }
    }

}
